// pages/biz/qualify/qualify.js
import util from '../../../../utils/util'
let goodsList = [
  { actEndTime: '2018-10-10 10:00:00' }
]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    countDownList: [],
    actEndTimeList: [],
    name:'',
    detail:'',
    imgUrls:'',
    productDeType: [4, 5, 6, 7],
    productHouseType: [1, 2, 3],
    flag:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(opt) {
    console.log(opt.time)
    let goodsList = [
      { actEndTime: opt.time }
    ]
    let endTimeList = [];
    // 将活动的结束时间参数提成一个单独的数组，方便操作
    goodsList.forEach(o => { endTimeList.push(o.actEndTime) })
    let tagList = opt.tags ? JSON.parse(opt.tags) : ''
    // if (tagList) {
    //   tagList = tagList.split(',')
    // }
    if (tagList && tagList.length >= 3) {
      tagList.splice(3, tagList.length - 1)
    }
    let seckillActivityId = wx.getStorageSync('seckillActivityId')
    this.setData({
      actEndTimeList: endTimeList, name: opt.name, detail: opt.detail, imgUrls: opt.imgUrls,    
      cityName: opt.cityName,
      seckillActivityId, 
      provinceName: opt.provinceName,
      tagList,
      seckillProductId: opt.id});
    // 执行倒计时函数
    //this.countDown();
  },
  // goDetail: function (e) {
  //   let { seckillProductId } = this.data
  //   let types = this.data.actionType
  //   wx.navigateTo({
  //     url: `/pages/biz/auctions/auctionDetail/auctionDetail?id=${seckillProductId}&type=${types}`
  //   })

  // },
  timeFormat(param) {//小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() {//倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime();
    let endTimeList = this.data.actEndTimeList;
    let countDownArr = [];

    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(o => {
      let endTime = util.dateFromString(o).getTime();
      let obj = null;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600) + (24 * day);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else {//活动已结束，全部设置为'00'
        obj = {
          day: '00',
          hou: '00',
          min: '00',
          sec: '00'
        }
        this.setData({
          flag:false
        })
        clearTimeout(this.data.timeOut)
      }
      countDownArr.push(obj);
      console.log(obj)
    })
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({ countDownList: countDownArr })
    this.data.timeOut = setTimeout(this.countDown, 1000);
    
  },
  router(e){
    let url = e.currentTarget.dataset.url
    let { seckillActivityId } = this.data
    if (url =='../orderList/orderList'){
      url = `${url}?seckillActivityId=${seckillActivityId}&tabIndex=0`
    }
    wx.reLaunch({
      url: url,
    })
  },
  toIndex(){
    wx.switchTab({
      url: '/pages/biz/index/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.countDown()
    let self = this
    wx.getStorage({
      key: 'currentDetail',
      success: function(res) {
        //console.log(res)
        self.setData({
          detail:res.data
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(this.data.timeOut)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.data.timeOut)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})